package com.soft.line.service.impl;

import com.github.pagehelper.PageInfo;
import com.soft.line.entity.ButtonInfo;
import com.soft.line.entity.MenuButtonInfo;
import com.soft.line.entity.MenuInfo;
import com.soft.line.mapper.ButtonInfoMapper;
import com.soft.line.mapper.MenuButtonInfoMapper;
import com.soft.line.mapper.MenuInfoMapper;
import com.soft.line.mapper.RoleMenuMapper;
import com.soft.line.service.MenuInfoService;
import com.soft.line.util.pagehelper.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class MenuInfoServiceImpl implements MenuInfoService {

    @Autowired
    private MenuInfoMapper menuInfoMapper;
    @Autowired
    private ButtonInfoMapper buttonInfoMapper;
    @Autowired
    private MenuButtonInfoMapper menuButtonInfoMapper;
    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Override
    public PageInfo<MenuInfo> selectAll(final Map<String,Object> map) {
        PageUtil.setPageParam(map);
        //String menuName = (String)map.get("menuName");
        List<MenuInfo> menuInfoList = menuInfoMapper.selectAll((String)map.get("menuName"),(String)map.get("menuUrl"));
        for (MenuInfo menuInfo:menuInfoList) {
            menuInfo.setButtonInfoList(selectButtonsByMenuId(menuInfo.getId()));
        }
        PageInfo<MenuInfo> menuInfoPage =new PageInfo(menuInfoList);
        return menuInfoPage;
    }

    @Override
    public List<ButtonInfo> selectButtonsByMenuId(Integer menuId) {
        return buttonInfoMapper.selectButtonsByMenuId(menuId);
    }

    @Override
    public MenuInfo selectByPrimaryKey(Integer id) {
        MenuInfo menuInfo = menuInfoMapper.selectByPrimaryKey(id);
        menuInfo.setButtonInfoList(buttonInfoMapper.selectButtonsByMenuId((id)));
        return menuInfo;
    }

    @Override
    public MenuInfo selectParentMenu(Integer pid) {
        return menuInfoMapper.selectParentMenu(pid);
    }

    @Override
    public int countMenuName(Integer id,String menuName) {
        return menuInfoMapper.countMenuName(id,menuName);
    }

    @Override
    public int countOrderBy(Integer id,String orderBy) {
        return menuInfoMapper.countOrderBy(id,orderBy);
    }

    @Override
    public List<String> selectALlMenuName() {
        return menuInfoMapper.selectALlMenuName();
    }

    @Override
    public void addMenuInfo(MenuInfo menuInfo) {
        menuInfoMapper.insert(menuInfo);
        for (ButtonInfo buttonInfo : menuInfo.getButtonInfoList()) {
            MenuButtonInfo menuButtonInfo = new MenuButtonInfo();
            menuButtonInfo.setMenuId(menuInfo.getId());
            menuButtonInfo.setButtonId(buttonInfo.getButtonId());
            menuButtonInfo.setModifyPerson(menuInfo.getModifyPerson());
            menuButtonInfo.setModifyTime(new Date());
            menuButtonInfoMapper.insert(menuButtonInfo);
        }
    }

    @Override
    public void updateMenInfo(MenuInfo menuInfo) {
        menuInfoMapper.update(menuInfo);
        List<String> oldButtonList = menuButtonInfoMapper.selectButtonIdByMenuId(menuInfo.getId());
        List<String> newButtonList = new ArrayList<String>();
        for (ButtonInfo bi:menuInfo.getButtonInfoList()){
            newButtonList.add(bi.getButtonId());
        }
        //旧的比新的多，删除旧的
        List<String> dealOldButtonList = getDiffrent(oldButtonList,newButtonList);
        for (String buttonId : dealOldButtonList) {
            roleMenuMapper.deleteByMenuIdAndButtonId(menuInfo.getId(),buttonId);
            menuButtonInfoMapper.deleteByMenuIdAndButtonId(menuInfo.getId(),buttonId);
        }
        //新的比旧的多，新增新的
        List<String> dealNewButtonList = getDiffrent(newButtonList,oldButtonList);
        for (String buttonId : dealNewButtonList) {
            MenuButtonInfo menuButtonInfo = new MenuButtonInfo();
            menuButtonInfo.setMenuId(menuInfo.getId());
            menuButtonInfo.setButtonId(buttonId);
            menuButtonInfo.setModifyPerson(menuInfo.getModifyPerson());
            menuButtonInfo.setModifyTime(new Date());
            menuButtonInfoMapper.insert(menuButtonInfo);
        }
    }

    @Override
    @Transactional
    public void delete(Integer id) {
        roleMenuMapper.deleteByMenuId(id);
        menuButtonInfoMapper.deleteByMenuId(id);
        menuInfoMapper.delete(id);
    }

    @Override
    public List<MenuInfo> selectTopMenu() {
        return menuInfoMapper.selectTopMenu();
    }

    @Override
    public List<MenuInfo> selectSonMenus() {
        List<MenuInfo> menuInfoList= menuInfoMapper.selectMenusInMenuButton();
        List<MenuInfo> resultList = new ArrayList<MenuInfo>();
        for (MenuInfo menuInfo : menuInfoList){
            menuInfo.setButtonInfoList(buttonInfoMapper.selectButtonsByMenuId(menuInfo.getId()));
            resultList.add(menuInfo);
        }
        return resultList;
    }

    //取两个list的差集
    private static List<String> getDiffrent(List<String> list1, List<String> list2) {
        List<String> list3 = new ArrayList<String>();
        list3.addAll(list1);
        list3.removeAll(list2);
        return list3;
    }
}
